//
//  MoreInputView.m
//  YunShouHu
//
//  Created by WangQiang on 16/6/23.
//  Copyright © 2016年 WangQiang. All rights reserved.
//

#import "MoreInputView.h"
#import "WQEdgeTitleButton.h"
@interface MoreInputView()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>

@end

@implementation MoreInputView

-(instancetype)init{
    if(self = [super init]){
        WQEdgeTitleButton *btn1 = [[WQEdgeTitleButton alloc] init];
        [btn1 setImage:[UIImage imageNamed:@"icon120_photoWrite"] forState:UIControlStateNormal];
        [btn1 setTitle:@"相册" forState:UIControlStateNormal];
         btn1.translatesAutoresizingMaskIntoConstraints = NO;
        btn1.titleAliment = ButtonTitleAlimentBottom;
        [btn1 setTitleColor:TITLE2 forState:UIControlStateNormal];
        [self addSubview:btn1];
        WQEdgeTitleButton *btn2 = [[WQEdgeTitleButton alloc] init];
        [btn2 setImage:[UIImage imageNamed:@"icon120_post_pic"] forState:UIControlStateNormal];
        [btn1 addTarget:self action:@selector(openAlbum) forControlEvents:UIControlEventTouchUpInside];
        [btn2 setTitle:@"拍摄" forState:UIControlStateNormal];
        [btn2 addTarget:self action:@selector(openCamera) forControlEvents:UIControlEventTouchUpInside];
        btn2.titleAliment = ButtonTitleAlimentBottom;
        [btn2 setTitleColor:TITLE2 forState:UIControlStateNormal];
        btn2.translatesAutoresizingMaskIntoConstraints = NO;
        [self addSubview:btn2];
        
        NSDictionary *views = NSDictionaryOfVariableBindings(btn1,btn2);
        NSDictionary *mertrics = @{@"btnH":@80,@"btnW":@60,@"paddingH":@20,@"paddingTop":@10,@"padingLeft":@10};
        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-padingLeft-[btn1(btnW)]-paddingH-[btn2(btnW)]" options:0 metrics:mertrics views:views]];
        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-paddingTop-[btn1(btnH)]-paddingTop-|" options:0 metrics:mertrics views:views]];
        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-paddingTop-[btn2(btnH)]-paddingTop-|" options:0 metrics:mertrics views:views]];
        
    }
    return self;
}

-(CGSize)intrinsicContentSize{
    return CGSizeMake(UIViewNoIntrinsicMetric, 100);
}
+(instancetype)moreInputView{
    return [[self alloc] init];
}

#pragma mark --打开照相机
- (void)openCamera{
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) return;
    UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
    ipc.sourceType = UIImagePickerControllerSourceTypeCamera;
    ipc.allowsEditing = YES;
    ipc.delegate = self;
    ipc.navigationBar.barTintColor = [UIColor blueColor];
    [[APPHELP currentNavgationController] presentViewController:ipc animated:YES completion:nil];
}

#pragma mark --打开相册
- (void)openAlbum{
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) return;
    UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
    ipc.allowsEditing = YES;
    ipc.navigationBar.barTintColor = [UIColor blueColor];
    ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    
    ipc.delegate = self;
    
    [[APPHELP currentNavgationController] presentViewController:ipc animated:YES completion:nil];
}


#pragma mark --UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    typeof(self) weakSelf = self;
    if([self.delegate respondsToSelector:@selector(moreInputViewDidFinshSelectedImage:imageURL:)]){
        NSURL *photoUrl = info[UIImagePickerControllerReferenceURL];
        [self.delegate moreInputViewDidFinshSelectedImage:info[UIImagePickerControllerOriginalImage] imageURL:photoUrl];
        
    }
    [[APPHELP currentNavgationController] dismissViewControllerAnimated:YES completion:^{
//        if([weakSelf.delegate respondsToSelector:@selector(moreInputViewDidFinshSelectedImage:)]){
//            [weakSelf.delegate moreInputViewDidFinshSelectedImage:info[UIImagePickerControllerOriginalImage]];
//        }
        
        do{
            if([weakSelf.delegate respondsToSelector:@selector(moreInputViewDidFinshSelectedImage:)]){
                [weakSelf.delegate moreInputViewDidFinshSelectedImage:info[UIImagePickerControllerOriginalImage]];
                break;
            }
            }while(NO);
        
        
        
    }];
  
}
@end
